import 'package:flutter/material.dart';

import 'main_my.dart';

void main() => runApp(MyApp());


var routes = {
  '/': (context)=>MyTabs(),
  '/form': (context)=>FormPage(),
  '/jump': (context)=>JumpPage(),
};
///StatelessWidget : 无状态组件
class MyApp extends StatelessWidget{

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return MaterialApp(
      initialRoute: "/",
      theme: ThemeData(
        primarySwatch: Colors.blueGrey,
      ),
      routes: routes,
    );
  }

}

///tab切换
class MyTabs extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return MyTabsState();
  }

}

class MyTabsState extends State<MyTabs>{

  int _currentIndex= 0;
  List<Widget> pages= new List();

  MyTabsState(){
    Widget page1 = new Page1();
    Widget page2 = new Page2();
    Widget page3 = new Page3();

    this.pages.add(page1);
    this.pages.add(page2);
    this.pages.add(page3);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('First Flutter Demo'),
      ),
      body: this.pages[this._currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: this._currentIndex,
        type: BottomNavigationBarType.fixed, //配置底部可以有多个按钮
        onTap: (int index){
          setState(() {
            this._currentIndex = index;
          });
        },
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: Text("首页"),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.apps),
            title: Text("应用"),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            title: Text("我的"),
          ),
        ],
      ),
    );
  }

}
class Page1 extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
      children: <Widget>[
        Text("page1"),
        MyButton("跳转", press: (){
          //导航到新路由
          Navigator.pushNamed(context, "/jump");
        },)
      ],
    );
  }

}
class Page2 extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Text("page2");
  }

}
class Page3 extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Text("page3");
  }

}

class JumpPage extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
      children: <Widget>[
        Text("jump page"),
        MyButton("continue", press: (){
          Navigator.pushNamed(context, "/form");
        },)
      ],
    );
  }
}

class FormPage extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
      children: <Widget>[
        Text("form page"),
        MyButton("continue", press: (){
          //点击后直接跳转到根路由
//          Navigator.of(context).pushAndRemoveUntil(
//            new MaterialPageRoute(builder: (context)=> new MyTabs()),
//              (route) =>route==null,
//          );
        },)
      ],
    );
  }

}